<template>
  <div class="certificate-info-wrap">
    <TitleBalance :name="`证书信息`"
                  :reason="reason"
                  :subtitle="'状态：'+ subtitle">
      <template v-if="certId != `0`"
                slot="right">
        <div class="download">
          <el-button type="primary"
                     :class="{disabled: !canClick}"
                     icon="el-icon-download"
                     @click="downloadCert">{{content}}</el-button>
        </div>
        <div class="tips">
          <i class="el-icon-warning-outline
            " />
          <span>仅支持下载三次(<i class="frequency">剩余{{count}}次</i>),下载后请妥善保管。</span>
        </div>
      </template>
      <!-- 内容 -->
      <template slot="center">
        <div v-if="certId"
             class="certificate-center">
          <el-carousel :autoplay="false"
                       :loop="false"
                       :height="carouselHeight"
                       :arrow="arrows">
            <el-carousel-item v-for="item in certImgs"
                              :key="item">
              <img ref="image"
                   :src="item"
                   class="cert-img"
                   @load="setCarouselHeight">
            </el-carousel-item>
          </el-carousel>
        </div>
        <cert-tbale v-else
                    :value="ereData" />
      </template>
    </TitleBalance>
  </div>
</template>

<script>
import cert from '@/api/modules/cert'
import CertTbale from './CertTbale.vue'
import TitleBalance from './TitleBalance.vue';
// import JSONbig from 'json-bigint'
export default {
  name: 'CertificateInfo',
  components: { TitleBalance, CertTbale },
  props: {
    value: {
      type: Object,
      default: () => { }
    }
  },
  data () {
    return {
      // urlPdf: [],
      // srcList: [],
      carouselHeight: null,
      certImgs: [],
      content: '下载证书文件',
      // count: 0,
      totalTime: 60,
      loading: true,
      canClick: false,
      downloadCount: 0,
      userInfo: sessionStorage.getItem('userInfo')
    }
  },
  computed: {
    user () {
      return JSON.parse(this.userInfo)
    },
    subtitle () {
      return this.certId ? `已生成` : `未生成`
    },
    certId () {
      return this.value?.certId
    },
    ereData () {
      return this.value?.ereData
    },
    reason () {
      return this.value?.failRecord
    },
    count () {
      return this.value?.count
    },
    arrows () {
      return this.certImgs?.length > 1 ? 'always' : 'never'
    }
  },
  created () {
    if (this.certId) {
      this.getCertImage()
    }
    this.canClick = this.count ? true : false //添加canClick录具体倒计时时间
  },
  methods: {
    downloadCert () {
      if (!this.canClick) return
      this.canClick = false
      this.getCertCheckDownload()
      this.content = this.totalTime + 's后重新下载'
      let clock = window.setInterval(() => {
        this.totalTime--
        this.content = this.totalTime + 's后重新下载'
        if (this.totalTime < 0) {
          window.clearInterval(clock)
          this.content = '下载证书文件'
          this.totalTime = 60
          this.canClick = true  //这里重新开启
        }
      }, 1000)
    },
    // 证书预览
    async getCertImage () {
      let response = await cert.getCertImage(this.certId)
      this.certImgs = response.data
      this.loading = false
      // this.setCarouselHeight()
    },
    // 下载证书文件
    async getCertCheckDownload () {
      let params = {
        // certId: this.$jsonBig(this.certId),
        certId: this.certId,
        downloadSource: 3,
        fileType: 2,
        // ip: 'http://192.168.1.43:8080',
        tenantId: this.user?.tenantId,
        tenantName: this.user?.tenantName,
        userId: this.user?.userId,
        userName: this.user?.userInfo?.name
      }
      let response = await cert.getCertCheckDownload(params)
      let { count, url } = response.data
      this.downloadCount = count
      // window.location.href = url
      window.open(url)
      this.$emit('downloadCount', this.downloadCount || 0)
    },
    setCarouselHeight () {
      this.$nextTick(() => {
        const image = this.$refs.image[0];
        const height = image.offsetHeight;
        this.carouselHeight = height + 'px';
      });
    }
  }
}
</script>

<style scoped>
.tips {
  padding-top: 12px;
  line-height: 16px;
}
.tips span {
  font-size: 12px;
  font-weight: 400;
  line-height: 16px;
  color: var(--prev-color-text-ashes);
}
.download {
  text-align: right;
}
.el-icon-warning-outline {
  color: var(--prev-color-primary);
  padding-right: 4px;
}
.certificate-center {
  text-align: center;
}
.certificate-center .cert-img {
  width: 100%;
  height: auto; /* 保证图片不失真 */
  object-fit: cover; /* 确保图片覆盖整个容器，但不失真 */
  /* object-fit: contain;
  height: 100%; */
}
.frequency {
  color: var(--prev-color-primary);
}
.disabled {
  background-color: var(--prev-border-color-extra-grey);
  border-color: var(--prev-border-color-extra-grey);
  color: var(--prev-bg-white);
  cursor: not-allowed;
}
</style>